#include <iostream>
using namespace std;

//Creating structure that can act as each node which hold both data and link to the other node

struct Node
{
	Node *link;   //link to the othe node
	int _sdata;   //data to hold in each node
};
Node* createNode(int);  //function to create a new Node
Node* pushNode(Node*,Node*);
Node* nextNode(Node*);
int main()
{
	Node *newNode,*existingNode,*foundNode;
	//declaring a pointer that can hold the address of a structure type (here the Node)

	char ans='y';
	int nodeCounter=0;
	while(ans=='y')
	{
		nodeCounter=nodeCounter+1;
		int data;
		cout<<"Enter Data to the Node\n";
		cin>>data;

		newNode=createNode(data);  //calling createNode and assigning the address of the created node to newNode pointer
		existingNode=pushNode(newNode,existingNode);//obtaining the modified new node and assigning it as existing node
		cout<<"Want to Add more nodes : (y/n)\n";
		cin>>ans;
	}
	ans='y';
	while(nodeCounter>0)
	{
		nodeCounter=nodeCounter-1;
		cout<<existingNode->_sdata<<endl;    //displaying the data in the existing node
		existingNode=nextNode(existingNode);
		//getting the node next to the existing node and then assigning it to existing node

	}

	return 0;
}


Node* createNode(int rval)   //Function to create new Node
{
	Node *rNewNode=new Node; // creating new temporary local node
	rNewNode->_sdata=rval;   //storing data
	rNewNode->link=NULL;     //pointing to a null pointer, which can hold the memory address of a structure type Node
	return(rNewNode);        // Returnig the just created new node
}

Node* pushNode(Node* rnewNode,Node* rexistingNode)   //Function to push new node to the stack
{
	rnewNode->link=rexistingNode; //assigning memory address of the existing node to the link of the rnewNode
	return(rnewNode);             //returning new node
}

Node* nextNode(Node *rExistingNode)
{
	Node *rNewNode=new Node;       //creating a new pointer to hold the memory address of the node next to the existing node
	rNewNode=rExistingNode->link;  //the memory address of the next node is obtained from the member link of the existing node
	return(rNewNode);              //returning the pointer which holds the memory address of the next node
}
